﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace LotteryTicket
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string savePath = "";

        /// <summary>  
        /// 选择保存文件的名称以及路径  取消返回 空"";  
        /// </summary>  
        /// <param name="fileName"></param>  
        /// <param name="filter"></param>  
        /// <param name="title"></param>  
        /// <returns></returns>  
        public static string SaveFilePathName(string fileName = null, string filter = null, string title = null)
        {
            string path = "";
            System.Windows.Forms.SaveFileDialog fbd = new System.Windows.Forms.SaveFileDialog();
            if (!string.IsNullOrEmpty(fileName))
            {
                fbd.FileName = fileName;
            }
            if (!string.IsNullOrEmpty(filter))
            {
                fbd.Filter = filter;// "Excel|*.xls;*.xlsx;";  
            }
            if (!string.IsNullOrEmpty(title))
            {
                fbd.Title = title;// "保存为";  
            }
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                path = fbd.FileName;
            }
            return path;
        }
        

        FileStream fs_output;
        StreamWriter sw_output;

        private void createstream()
        {
            fs_output = new FileStream(savePath, FileMode.Create);
            sw_output = new StreamWriter(fs_output);
        }
        private void closestream()
        {
            if (sw_output != null)
                sw_output.Close();
            if (fs_output != null)
                fs_output.Close();//这里要注意fs一定要在sw后面关闭，否则会抛异常
        }
        private void writefile(string line)
        {
            sw_output.WriteLine(line);
        }
        public void Refresh(string path)
        {
            StreamReader sr = new StreamReader(path, Encoding.Default);
            String line;
            int index = 0;
            int flag = 0;
            createstream();
            
            while ((line = sr.ReadLine()) != null)
            {
               // MessageBox.Show(line.ToString());

                if (line == "#")
               {
                   flag ++;
                   //if (sArray[1] == "start")
                   //{
                   //    index = 0;
                   //    writefile(line);
                   //}
                   if(flag == 2)
                   {
                       flag = 0;
                       
                   }
                   writefile(line);
               }
                else if (flag == 0)
                {
                    //string[] sArray = line.Split(' ');
                    index++;
                    writefile(index + " " + line);
                }else {
                    writefile(line);
                    if (line == "end")
                    {
                        writefile("total");
                        writefile(index.ToString());
                    }
                }

               // break;
            }

            closestream();
            MessageBox.Show("OK");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Refresh("Resource/source.txt");//"E:/CODE/C#/Lottery-Ticket/LotteryTicket/Resource/14to18.txt");
            FileHandle fh = new FileHandle();
            int x = 0;
           // fh.CalculateCircle10(1);
           // fh.CalculateCircle5_max(1);
            fh.CalculateAppear(500);
            fh.GetAppear(500,"disappear");
          //  fh.CalculateAppear_p(500);
            //fh.CalculateSpan();
          //  fh.CalculateSpan_p();
            fh.GetAppearFilter(500);
            MessageBox.Show("OK");
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            savePath = SaveFilePathName("output.txt", "txt文件(*.txt)|*.txt");
        }

        private void output_data( )
        {
           
            if (savePath.Equals(""))
            {
                MessageBox.Show("请选择保存路径！");
                return;
            }
            List<String> list = new List<string>();
            List<String> list_after = new List<string>();
            createstream();


           
            closestream();
            
            MessageBox.Show("操作完成！");

        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            output_data();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int id =1735;
            dbOperate dbo = new dbOperate("Data Source=E:/CODE/C#/Lottery-Ticket/LotteryTicket/Resource/lt.db");

            MessageBox.Show("OK");
        }
    }
}
